package Tree;

public class ArrBinaryTree {
    public static void main(String[] args) {
        int arr [] = {1,2,3,4,5,6,7};
        ArrBinaryTree1 tree1 = new ArrBinaryTree1(arr);
        System.out.println("前序遍历");
        tree1.preOrder();
        System.out.println();
        System.out.println("中序遍历");
        tree1.minOrder();
        System.out.println();
        System.out.println("后序遍历");
        tree1.latterOrder();

    }
}
class ArrBinaryTree1{
    private int arr [];

    public ArrBinaryTree1(int[] arr) {
        this.arr = arr;
    }
    public void preOrder(){
        preOrder(0);
    }
    public void preOrder(int index){
        if (arr!=null){
            System.out.print(arr[index]);
            if (2*index+1<arr.length){
                preOrder(2*index+1);
            }
            if(2*index+2<arr.length){
                preOrder(2*index+2);
            }
        }else {
            System.out.println("为空");
        }
    }
    public void  minOrder(){
        minOrder(0);
    }
    //中序遍历
    public void minOrder(int index){
        if (arr!=null){
            if (2*index+1<arr.length){
                minOrder(2*index+1);
            }
            System.out.print(arr[index]);
            if(2*index+2<arr.length){
                minOrder(2*index+2);
            }
        }else {
            System.out.println("为空");
        }
    }
    public void latterOrder(){
        latterOrder(0);
    }
    public void latterOrder(int index){
        if (arr!=null){
            if (2*index+1<arr.length){
                latterOrder(2*index+1);
            }
            if(2*index+2<arr.length){
                latterOrder(2*index+2);
            }
            System.out.print(arr[index]);
        }else {
            System.out.println("为空");
        }
    }
}
